package wojtek.ckm;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class KlubGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 * @param klub 
	 */
	public KlubGUI(final Klub klub) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel = new JLabel("Nazwa: " + klub.getNazwaKlubu());
		
		contentPane.add(lblNewLabel, "6, 2");
		
		JLabel lblNazwaDruzyny = new JLabel("nazwa druzyny");
		contentPane.add(lblNazwaDruzyny, "4, 5, right, default");
		
		textField = new JTextField();
		contentPane.add(textField, "6, 5, fill, default");
		textField.setColumns(10);
		
		JLabel lbltrener = new JLabel("trener");
		contentPane.add(lbltrener, "4, 7, right, default");
		
		textField_1 = new JTextField();
		contentPane.add(textField_1, "6, 7, fill, default");
		textField_1.setColumns(10);
		
		JLabel lblDruzyny = new JLabel("Druzyny");
		contentPane.add(lblDruzyny, "6, 9");
		
		final JList list = new JList();
		contentPane.add(list, "6, 11, fill, fill");
		list.addListSelectionListener(new ListSelectionListener(){

			@Override
			public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false){

	int numer = list.getSelectedIndex();
	textField.setText(klub.zwrocDruzyne(numer).getNazwa());
	textField_1.setText(klub.zwrocDruzyne(numer).getTrener());
}
			}
		});
		
		
		
		JButton btnDodajDruzyna = new JButton("Dodaj Druzyna");
		contentPane.add(btnDodajDruzyna, "4, 15");

		btnDodajDruzyna.addActionListener(new ActionListener() { /**....................dodawanie druzyn */
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("druzyn");
						
				dodajDruzyne(klub);
				list.setListData(klub.getDruzyny());

			}
		});
		
		
		
		JButton btnEdytujDruzyna = new JButton("Edytuj Druzyna"); /**....................edytowanie druzyn */
		contentPane.add(btnEdytujDruzyna, "6, 15");
		
		btnEdytujDruzyna.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
	
		
				edytujDruzyna(klub.zwrocDruzyne(list.getSelectedIndex()));

			}});
		
		
		
		JButton btnUsunDruzyna = new JButton("Usun Druzyna"); /**....................usuwanie druzyn */
		contentPane.add(btnUsunDruzyna, "8, 15");
		
		btnUsunDruzyna.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
	
		usunDruzyna(klub, list);
		list.setListData(klub.getDruzyny());

			}});
		
	}
	public void dodajDruzyne(Klub klub){
		Druzyna druzyna = new Druzyna(textField.getText(), textField_1.getText());
		klub.dodajDruzyna(druzyna);
	}
	
	public void usunDruzyna(Klub klub, JList list){
		System.out.println(list.getSelectedIndex());

		int usuwanaDruzyna = list.getSelectedIndex();
		klub.usunDruzyne(usuwanaDruzyna);
	}
	
	public void edytujDruzyna(final Druzyna druzyna){
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DruzynaGUI frame = new DruzynaGUI(druzyna);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
}
